<?php

namespace zhtfrontend\models\datas;

use common\models\datas\Customer;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\ActiveRecord;
use yii\helpers\ArrayHelper;

/**
 * This is the model class for table "{{%classmanage_turn}}".
 *
 * @property integer $id
 * @property integer $customer_id
 * @property integer $student_id
 * @property integer $old_class_manage_id
 * @property integer $new_class_manage_id
 * @property string $content
 * @property integer $created_at
 * @property integer $admin_id
 */
class ClassmanageTurn extends \zhtfrontend\components\bases\BaseModel
{
    public $cover_lesson;

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['customer_id', 'student_id', 'old_class_manage_id', 'new_class_manage_id', 'created_at', 'admin_id','lesson','cover_lesson','num'], 'integer'],
            [['content'], 'string', 'max' => 255],
            [['new_class_manage_id','content','lesson','num'],'required']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => Yii::t('zhtfrontend', '自增id'),
            'customer_id' => Yii::t('zhtfrontend', '会员id'),
            'student_id' => Yii::t('zhtfrontend', '学人生id'),
            'old_class_manage_id' => Yii::t('zhtfrontend', '旧班id'),
            'new_class_manage_id' => Yii::t('zhtfrontend', '转入班级'),
            'content' => Yii::t('zhtfrontend', '转班原因'),
            'created_at' => Yii::t('zhtfrontend', '转班时间'),
            'admin_id' => Yii::t('zhtfrontend', '操作人id'),
            'lesson' => Yii::t('zhtfrontend', '从哪一节调入'),
            'cover_lesson' => Yii::t('zhtfrontend', '选择被调课次'),
            'num' => Yii::t('zhtfrontend', '转入课次数'),
        ];
    }



    public function behaviors()
    {
        return ArrayHelper::merge(parent::behaviors(), [
            [
                'class' => TimestampBehavior::className(),
                'attributes' => [
                    ActiveRecord::EVENT_BEFORE_INSERT => ['created_at'],
                ],
            ],

        ]);
    }

    public function fields()
    {
        $fields = parent::fields();
        $fields['created_at'] = function(){
            return Yii::$app->formatter->asDatetime($this->created_at);
        };
        return $fields;
    }

    public function extraFields()
    {
        $fields = parent::extraFields(); // TODO: Change the autogenerated stub
        $fields['customer_name'] = function (){
            return isset($this->customer)?$this->customer->name:'';
        };
        $fields['old_course_name'] = function (){
            Yii::error('oldclassmanage---');
            Yii::error($this->oldclassmanage);
            $customer = $this->oldclassmanage->toArray([],['course_name']);
            return $customer['course_name'];
        };
        $fields['new_course_name'] = function (){
            $customer = $this->oldclassmanage->toArray([],['course_name']);
            return $customer['course_name'];
        };
        $fields['old_teacher_name'] = function (){
            $old_teacher = $this->oldclassmanage->toArray([],['teacher_name']);
            return $old_teacher['teacher_name'];
        };
        return $fields;
    }

    public function getCustomer(){
        return $this->hasOne(Customer::className(),['id'=>'customer_id']);
    }

    public function getOldclassmanage(){
        return $this->hasOne(ClassManage::className(),['class_id'=>'old_class_manage_id']);
    }
    public function getNewclassmanage(){
        return $this->hasOne(ClassManage::className(),['class_id'=>'new_class_manage_id']);
    }
}
